package com.lovo.roomProject.room.dao.impl;

import com.lovo.roomProject.dao.BaseDAO;
import com.lovo.roomProject.pojo.CutPageBean;
import com.lovo.roomProject.pojo.RoomDO;
import com.lovo.roomProject.pojo.RoomSetDO;
import com.lovo.roomProject.room.dao.RoomSetDAO;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class RoomSetDAOImpl extends BaseDAO implements RoomSetDAO {
    private RoomSetDO fillRoomSet() throws SQLException {
        RoomSetDO roomSetDO = new RoomSetDO();
        roomSetDO.setId(rs.getInt("rs_id"));
        roomSetDO.setRoomId(rs.getInt("rs_room_id"));
        roomSetDO.setDescription(rs.getString("rs_description"));
        roomSetDO.setErrorDate(LocalDate.parse(rs.getString("rs_error_date")));
        roomSetDO.setIsPressed(rs.getString("rs_is_pressed"));
        return roomSetDO;
    }

    @Override
    public CutPageBean listRoomsSetByPage(int current, int roomID, String isPressed) {
        this.getConnection();
        String selectSql = "select * ";
        String countSql = "select count(*) cnt ";
        String fromSql = "from t_room_set ";
        String limitSql = "limit ?,?";

        String dynamicSql = "where 1=1 ";
        if (roomID != 0) {
            dynamicSql += "and rs_room_id=" + roomID + " ";
        }
        if (isPressed != null && !isPressed.equals("")) {
            dynamicSql += "and rs_is_pressed='" + isPressed + "' ";
        }

        CutPageBean cutPageBean = new CutPageBean();
        List list = new ArrayList();
        try {
            statement = con.prepareStatement(selectSql + fromSql + dynamicSql + limitSql);
            statement.setObject(1, (current - 1) * CutPageBean.PAGESIZE);
            statement.setObject(2, CutPageBean.PAGESIZE);
            rs = statement.executeQuery();
            while (rs.next()) {
                RoomSetDO roomSetDO = fillRoomSet();
                list.add(roomSetDO);
            }
            statement = con.prepareStatement(countSql + fromSql + dynamicSql);
            rs = statement.executeQuery();
            rs.next();
            cutPageBean.setCount(rs.getInt("cnt"));
            cutPageBean.setList(list);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeConnection();
        }
        return cutPageBean;
    }

    @Override
    public RoomSetDO getRoomSetById(int id) {
        this.getConnection();
        String sql = "select * from t_room_set where rs_id=? ";
        CutPageBean cutPageBean = new CutPageBean();
        RoomSetDO roomSetDO = null;
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            rs = statement.executeQuery();
            if (rs.next()) {
                roomSetDO = fillRoomSet();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeConnection();
        }
        return roomSetDO;
    }

    @Override
    public void insert(RoomSetDO roomSetDO) {
        this.getConnection();
        String sql = "insert into t_room_set(rs_room_id,rs_description,rs_error_date,rs_is_pressed) value(?,?,?,?) ";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, roomSetDO.getRoomId());
            statement.setObject(2, roomSetDO.getDescription());
            statement.setObject(3, roomSetDO.getErrorDate().toString());
            statement.setObject(4, roomSetDO.getIsPressed());
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeConnection();
        }
    }


    @Override
    public void update(RoomSetDO roomSetDO) {
        this.getConnection();
        String sql = "update t_room_set set rs_is_pressed=? where rs_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, roomSetDO.getIsPressed());
            statement.setObject(2, roomSetDO.getId());
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeConnection();
        }
    }
}
